package com.jiejie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiejie.commons.ServerResponse;
import com.jiejie.entity.Comments;
import com.jiejie.entity.Movie;
import com.jiejie.entity.MovieAndCast;
import com.jiejie.entity.Order;
import com.jiejie.mapper.CommentsMapper;
import com.jiejie.mapper.MovieAndCastMapper;
import com.jiejie.mapper.MovieMapper;
import com.jiejie.mapper.OrderMapper;
import com.jiejie.service.IMovieService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 辰广
 * @since 2022-10-05
 */
@Service
public class MovieServiceImpl extends ServiceImpl<MovieMapper, Movie> implements IMovieService {

    @Resource
    private MovieMapper movieMapper;
    @Resource
    private CommentsMapper commentsMapper;

    @Resource
    private OrderMapper orderMapper;


    @Cacheable(cacheNames = "hotMovie",key = "'hot_movie'")
    @Override
    public List<Movie> getHotMovie() {
        //1.redis
        //2.mysql--->redis(expire 24h)
        QueryWrapper<Movie> queryWrapper=new QueryWrapper();
        queryWrapper.orderByAsc("movie_openhours");
       // queryWrapper.eq("movie_status",0);
        queryWrapper.select("movie_id","movie_cname","movie_picture");
        //设置分页信息，查询第一页，每页显示8条
        Page<Movie> page=new Page<>(1,8);
        Page<Movie> pageInfo=movieMapper.selectPage(page,queryWrapper);
        List<Movie> movieList=pageInfo.getRecords();
        System.out.println("从mysql中查询出来的热门电影："+movieList);
        return movieList;
    }

    @Override
    public List<Movie> getExpectMovie() {
        QueryWrapper<Movie> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("movie_status",1);
        queryWrapper.select("movie_id","movie_cname","movie_picture");
        Page<Movie> page=new Page<>(1,8);
        Page<Movie> pageInfo=movieMapper.selectPage(page,queryWrapper);
        List<Movie> movieList=pageInfo.getRecords();
        System.out.println("从mysql中查询出来的预售电影："+movieList);
        return movieList;
    }

    @Override
    public Movie getMovieDetailById(Integer movieId) {
        Movie movie=movieMapper.selectById(movieId);
        return movie;
    }

    @Override
    public ServerResponse getMovieFilms(Integer movieStatus, String movieType, String movieArea, String movieOpenhours,Integer page) {
        QueryWrapper<Movie> queryWrapper = new QueryWrapper<>();

        if (movieStatus ==0){
            LocalDate now=LocalDate.now();
            queryWrapper.le("movie_openhours",now);
            queryWrapper.ge("movie_openhours",LocalDate.of(now.getYear()-5,now.getMonth(),now.getDayOfMonth()));
        }else if (movieStatus ==1){
            queryWrapper.ge("movie_openhours",LocalDate.now());
        }else {
            LocalDate now=LocalDate.now();
            queryWrapper.le("movie_openhours",LocalDate.of(now.getYear()-5,now.getMonth(),now.getDayOfMonth()));
        }
        if (!movieType.equals("全部"))
            queryWrapper.like("movie_type",movieType);
        if (!movieArea.equals("全部"))
            queryWrapper.eq("movie_area",movieArea);
        if (!movieOpenhours.equals("全部")){
            queryWrapper.like("movie_openhours",movieOpenhours);
        }

        queryWrapper.select("movie_id","movie_cname","movie_picture","movie_status");
        System.out.println(page);
        Page<Movie> pa = new Page<>(page,12);
        Page<Movie> pageInfo = movieMapper.selectPage(pa,queryWrapper);
        System.out.println(">>>>>>>>>"+pageInfo);

        if (pageInfo!=null)
            return ServerResponse.success("查询成功",pageInfo);
        return ServerResponse.fail("查询成功",null);
    }



    @Override
    public Map<Order, Movie> getOrderMovie(Integer customerId) {
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("customer_id",customerId);
        List<Order> order = orderMapper.selectList(wrapper);
        Map<Order,Movie> map = new HashMap<>();
        for (int i=0 ;i<order.size();i++){
            Movie movie = movieMapper.selectById(order.get(i).getMovieId());
            map.put(order.get(i),movie);
        }
        return map;
    }

    @Override
    public List<Movie> getAllMovie() {
        return movieMapper.selectList(null);
    }


}
